<?php


namespace app\api\controller\ShopGoods;



use app\api\controller\BaseController;
use app\api\model\Goods\GoodsModel;
use app\api\model\ShopGoods\ShopGoodsAttrModel;
use app\api\model\ShopGoods\ShopGoodsAttrValueModel;
use app\api\model\ShopGoods\ShopGoodsCateModel;
use app\api\model\ShopGoods\ShopGoodsModel;
use app\Request;
use app\Utils\ImageUtil;
use app\Utils\Util;

class ShopGoods extends BaseController
{

    /**
     * @api {get} /get_shop_goods_cate_more 积分商城商品分类
     * @apiGroup 积分商城
     * @apiSuccess {int} code 状态码 0：正常 -1：错误
     * @apiSuccess {json} data 数据源
     * @apiSuccess {string} msg 描述
     * @apiVersion 1.0.0
     */
    public function getShopGoodsCateMore(Request $request)
    {
        $list = [];
        $data = ShopGoodsCateModel::getGoodsCateWhereMore([['is_show','=',1],['pid','=',0]]);
        foreach ($data as $item){
            $arr['id'] = $item['id'];
            $arr['cate_name'] = $item['cate_name'];
            $arr['img'] = ImageUtil::i()->replaceUrl($item['img']);
            array_push($list,$arr);
        }
        return Util::returnJson(self::CODE_SUCCESS,$list);
    }

    /**
     * @api {get} /get_shop_goods_list 积分商城商品列表
     * @apiGroup 积分商城
     * @apiParam {int} page 页码
     * @apiParam {int} limit 数据条数
     * @apiParam {string} good_name 搜索商品名字
     * @apiParam {int} cate_id 商品分类ID
     * @apiParam {int} sort 排序 (1价格升序 2价格降序,3销量升序 4销量降序)
     * @apiSuccess {int} code 状态码 0：正常 -1：错误
     * @apiSuccess {json} data 数据源
     * @apiSuccess {string} msg 描述
     * @apiVersion 1.0.0
     */
    public function getShopGoodsList(Request $request)
    {
        $param = $request->param();
        $where = [
            ['is_show', '=', 1],
            ['is_del', '=', 0]
        ];
        $page = !empty($param['page']) ? $param['page'] : 1;
        $limit = !empty($param['limit']) ? $param['limit'] : 10;
        $good_name = !empty($param['good_name']) ? $param['good_name'] : "";
        //分类
        $cate_id = !empty($param['cate_id']) ? $param['cate_id'] : 0;
        if (!empty($good_name)) {
            array_push($where, ['g.good_name', 'like', '%' . $good_name . '%']);
        }
        if(!empty($cate_id)){
            array_push($where, ['gcr.cate_id', '=', $cate_id]);
        }

        //排序 (1价格升序 2价格降序,3销量升序 4销量降序)
        $sort = !empty($param['sort']) ? $param['sort'] : "";

        $list = ShopGoodsModel::getGoodsListWhere($page, $limit, $where, $sort,'g.id,g.good_name,g.img,g.slider_img,g.price,g.vip_price,g.integral,g.is_postage,g.postage,g.is_hot,g.is_new,g.is_benefit,g.details,g.sales,g.ficti,g.browse');
        $count = GoodsModel::getGoodsCountWhere($where);
        foreach ($list as &$item){
            $item['img'] = ImageUtil::i()->replaceUrl($item['img']);
            $images = !empty($item['slider_img']) ? json_decode($item['slider_img'],true) : [];
            foreach ($images as &$image){
                $image = ImageUtil::i()->replaceUrl($image);
            }
            $item['slider_img'] = $images;
            $item['details'] = !empty($item['details']) ? htmlspecialchars_decode($item['details']) : "";
        }
        return Util::returnPageJson(self::CODE_SUCCESS, $count, $list, "");
    }

    /**
     * @api {get} /get_shop_goods_like_list 积分商城猜你喜欢
     * @apiGroup 积分商城
     * @apiSuccess {int} code 状态码 0：正常 -1：错误
     * @apiSuccess {json} data 数据源
     * @apiSuccess {string} msg 描述
     * @apiVersion 1.0.0
     */
    public function getShopGoodsLikeList(Request $request)
    {
        $where = [
            ['is_show', '=', 1],
            ['is_del', '=', 0]
        ];
        $list = ShopGoodsModel::getRandomGoods(6, $where);
        foreach ($list as &$item){
            $item['img'] = ImageUtil::i()->replaceUrl($item['img']);
            $images = !empty($item['slider_img']) ? json_decode($item['slider_img'],true) : [];
            foreach ($images as &$image){
                $image = ImageUtil::i()->replaceUrl($image);
            }
            $item['slider_img'] = $images;
            $item['details'] = !empty($item['details']) ? htmlspecialchars_decode($item['details']) : "";
        }
        return Util::returnJson(self::CODE_SUCCESS, $list);
    }

    /**
     * @api {get} /get_shop_goods_info 积分商城商品详情
     * @apiGroup 积分商城
     * @apiParam {int} id 商品ID
     * @apiSuccess {int} code 状态码 0：正常 -1：错误
     * @apiSuccess {json} data 数据源
     * @apiSuccess {string} msg 描述
     * @apiVersion 1.0.0
     */
    public function getShopGoodsInfo(Request $request)
    {
        $param = $request->param();
        $goods = ShopGoodsModel::getGoodsWhere([['id','=',$param['id']]],'');
        $goods['img'] = ImageUtil::i()->replaceUrl($goods['img']);
        $images = !empty($goods['slider_img']) ? json_decode($goods['slider_img'],true) : [];
        foreach ($images as &$image){
            $image = ImageUtil::i()->replaceUrl($image);
        }
        $goods['slider_img'] = $images;
        $goods['details'] = !empty($goods['details']) ? htmlspecialchars_decode($goods['details']) : "";

        if(!empty($goods['sales_setting'])){
            $sales_setting = json_decode($goods['sales_setting'],true);
            $goods['level'] = $sales_setting['level'];
            $goods['level_sales0'] = $sales_setting['level_sales0'];
            $goods['level_sales1'] = $sales_setting['level_sales1'];
            $goods['level_sales2'] = $sales_setting['level_sales2'];
        }else{
            $goods['level'] = 0;
            $goods['level_sales0'] = 0;
            $goods['level_sales1'] = 0;
            $goods['level_sales2'] = 0;
        }

        $attr = ShopGoodsAttrModel::getGoodsAttrWhereMore([['goods_id','=',$goods['id']]]);
        if(!empty($attr)){
            foreach ($attr as &$it){
                $it['attr_values'] = explode(",",$it['attr_values']);
            }
            $goods['attr'] = $attr;
            $goods['attr_value'] = ShopGoodsAttrValueModel::getGoodsAttrValueWhereMore([['goods_id','=',$goods['id']]]);
            //组装数据
            foreach ($goods['attr_value'] as &$item){
                $suk = explode(",",$item['suk']);
                foreach ($attr as $k=>$val){
                    $item[$val['attr_name']] = $suk[$k];
                }
            }
        }
        return Util::returnJson(self::CODE_SUCCESS, $goods, "");
    }

}
